//
//  PayTicketViewController.m
//  Mtime
//
//  Created by wang xinkai on 15/7/28.
//  Copyright (c) 2015年 wxk. All rights reserved.
//

#import "PayTicketViewController.h"
#import "CinemaModel.h"
#import "CinemaTableViewCell.h"
#import "SegmentView.h"
#import "BottomView.h"//底部弹出视图


@implementation PayTicketViewController
{

    UITableView *_cinemaTableView;//影院列表

    NSMutableArray *_cinemaList;//影院列表数据
    
    BottomView *_bottomView;//底部弹出视图
    
    SegmentView *seg;
    
}
-(id)init{
    
    if (self = [super init]) {
        
        self.title = @"影院";
        self.tabBarItem.image = [UIImage imageNamed:@"store"];
        
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"star_bg"]];
    }
    return self;
}

- (void)viewDidLoad {

    [super viewDidLoad];

    [self creatSegmentView];//添加按钮栏
    
    [self loadData];//加载数据
    /*
     *如果后执行加载数据的方法的话 注意将table view  reloaddata
     *涉及到传值 传值的方法在后执行的方法中写｛只有两个方法与传值相关 1，加载数据的方法  2，加载视图｝
     */
    
    [self creatTableView];//添加影院列表
    
    [self creatBottomView];//加载底部弹出视图
    
}


//根据坐标位置去刷新table view的数据
- (void)reloadData1 {

    //设置我的地理坐标
    float currentLocation = 116.00;

    for (int i = 0; i < _cinemaList.count; i ++) {
        
        for (int j = i +1; j < _cinemaList.count; j ++) {
            
            CinemaModel *model1 = _cinemaList[i];
            CinemaModel *model2 = _cinemaList[j];
            
            float x1 = [model1.longitude floatValue] -currentLocation;
            float x2 = [model2.longitude floatValue] -currentLocation;
            
            if (x1 > x2) {
                
                [_cinemaList exchangeObjectAtIndex:i withObjectAtIndex:j];
            }
        }
    }
    [_cinemaTableView reloadData];
}

- (void)reloadData2 {

    for (int i = 0; i < _cinemaList.count; i ++) {
        
        for (int j = i +1; j < _cinemaList.count; j ++) {
            
            CinemaModel *model1 = _cinemaList[i];
            CinemaModel *model2 = _cinemaList[j];
            
            float x1 = [model1.minPrice floatValue];
            float x2 = [model2.minPrice floatValue];
            
            if (x1 > x2) {
                
                [_cinemaList exchangeObjectAtIndex:i withObjectAtIndex:j];
            }
        }
    }
    [_cinemaTableView reloadData];

}

- (void)creatBottomView {

    _bottomView = [[[NSBundle mainBundle] loadNibNamed:@"BottomView" owner:nil options:nil] lastObject];

    _bottomView.frame = CGRectMake(0, kScreenH -49 -64, kScreenW, 500);
    [self.view addSubview:_bottomView];
    
}

- (void)showBottomView {

    [UIView animateWithDuration:.2 animations:^{
       
        _bottomView.transform = CGAffineTransformMakeTranslation(0, -300);
        
        _bottomView.segmentView.selectedIndex = 0;
        
    }];

}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    
    seg.selectedIndex = 0;

}

- (void)hiddenBottomView {

    [UIView animateWithDuration:.2 animations:^{
        
        _bottomView.transform = CGAffineTransformIdentity;
    }];


}

- (void)reloadTableViewData:(NSInteger)index {

    switch (index) {
        case 0:
            [self loadData];
            [self hiddenBottomView];
            break;
        case 1:
            [self reloadData1];
            [self hiddenBottomView];
            break;
        case 2:
            [self reloadData2];
            [self hiddenBottomView];
            break;
        case 3:
            [self showBottomView];
            break;
        default:
            break;
    }
    

}

- (void)creatSegmentView {

    seg = [[SegmentView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 50) WithTitiles:@[@"全部",@"附近",@"价格",@"筛选"]];
    
    seg.selectImageName = @"icon_slider_min";
    [seg setBackgroundColor:[UIColor clearColor]];
    
    //桥接  创建一个block代码块中可以使用的self
    __block PayTicketViewController *blockSele = self;
    [seg addBlcok:^(NSInteger index) {
       
        [blockSele reloadTableViewData:index];
        
    }];
    
    [self.view addSubview:seg];

}

- (void)loadData {

    _cinemaList = [[NSMutableArray alloc] init];
    
    
    //1.通过json的解析类CoreDataSource解析json数据
    //json数据的保存形式是什么就用什么来接收
    NSArray *cinemaArray = [CoreDataSource initWithDataSource:@"cinema"];
    
    //1.1获取目标数据所在的数组
    //.......
    
    //2.遍历目标数据所在的数组
    for (NSDictionary *dic in cinemaArray) {
        
        //3.初始化一个对应的model  将目标数据dic传入model
        //3.1在model中写入需要参数的属性名 属性名注意尽量与dic中的key值一一对应 
        
        CinemaModel *model = [[CinemaModel alloc] initWithDic:dic];
        
        //4.将model数据存入数组-->实现讲model和cell一一对应
        //4.1 提前将存放model数据的数组初始化
        [_cinemaList addObject:model];
    }
    
    [_cinemaTableView reloadData];
}

- (void)creatTableView {

    _cinemaTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, kScreenW, kScreenH -64 -50)];
    
    _cinemaTableView.dataSource = self;
    _cinemaTableView.delegate = self;
    _cinemaTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_cinemaTableView];

}


#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _cinemaList.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CinemaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cinemaCell"];
    
    if (!cell) {
        
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cinemaCell"];
        
        cell = [[[NSBundle mainBundle]loadNibNamed:@"CinemaTableViewCell" owner:nil options:nil] objectAtIndex:0];
    }
    
    //5.将model数据传入cell
    /*
     *table view 的单元格个数是根据model数组的长度来获得的  所以第几个cell的model数据就来源于model数组的第几个model
     */
    cell.model = _cinemaList[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 150;

}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    CinemaTableViewCell *cinemaCell = (CinemaTableViewCell *)cell;
    for (UIView *view in cinemaCell.contentView.subviews) {
        
        view.transform = CGAffineTransformIdentity;
        
        view.hidden = NO;
    }
    

}

@end
